package  com.bs.medicine.struts.action;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.axis2.AxisFault;
import org.apache.struts2.ServletActionContext;

import com.bs.entity.MedicineCategory;
import com.bs.entity.MedicineInfo;
import com.mysoap.medicine.service.CategorySoapStub;
import com.mysoap.medicine.service.MedicineInfoSoapStub;
import com.mysoap.medicine.service.CategorySoapStub.CategoryQuery;
import com.mysoap.medicine.service.CategorySoapStub.CategoryQueryResponse;
import com.mysoap.medicine.service.CategorySoapStub.CategoryRequest;
import com.mysoap.medicine.service.CategorySoapStub.CategoryResponse;
import com.mysoap.medicine.service.MedicineInfoSoapStub.MedicineInfoResponse;
import com.opensymphony.xwork2.ActionSupport;

public class CategoryAction extends ActionSupport {
	private MedicineCategory medicineCategory;
	private static CategorySoapStub categorySoapStub;
//	private CategoryService categoryService;
	private List<MedicineCategory> list;
	private String categoryid;
	
	public String getCategoryid() {
		return categoryid;
	}

	public void setCategoryid(String categoryid) {
		this.categoryid = categoryid;
	}


	public List<MedicineCategory> getList() {
		return list;
	}

	public void setList(List<MedicineCategory> list) {
		this.list = list;
	}

	public MedicineCategory getMedicineCategory() {
		return medicineCategory;
	}

	public void setMedicineCategory(MedicineCategory medicineCategory) {
		this.medicineCategory = medicineCategory;
	}
	
	static{
		try {
			String address = "http://localhost:8080/MySoap/services/CategorySoap";
			categorySoapStub  = new CategorySoapStub(address);
		} catch (AxisFault e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
    
	/**
	 * 添加药品类别
	 * @return
	 * @throws Exception
	 */
	public String addCategory() throws Exception{
//		categoryService.addMedicineCategory(medicineCategory);
		return SUCCESS;
	}
     
	/**
	 * 查询药品类别列表
	 * @return
	 * @throws Exception
	 */
	public String queryCategory() throws Exception{
		CategoryQuery categoryQuery = new CategoryQuery();
		CategoryRequest param = new CategoryRequest();
		param.setUpdateFlag(2);
		categoryQuery.setRequest(param );
		CategoryQueryResponse queryresponse = categorySoapStub.categoryQuery(categoryQuery );
		CategoryResponse response = queryresponse.get_return();
		list = ToList(response);
		System.out.println(list.get(0).getCategoryName());
//		list =categoryService.getAllMedicineCategory();
		return SUCCESS;
	}
	
	/**
	 * 编辑药品类别前
	 * @return
	 * @throws Exception
	 */
	public String editCategory() throws Exception{
	//	medicineCategory=categoryService.getCategoryById(categoryid);
		return SUCCESS;
	}
	
	/**
	 * 编辑保存药品信息
	 * @return
	 * @throws Exception
	 */
	public String editSaveCategory() throws Exception{
		
		HttpServletRequest request =ServletActionContext.getRequest();
		String categoryid =request.getParameter("categoryid");
		String categoryName =request.getParameter("categoryName");
		String categoryDesc =request.getParameter("categoryDesc");
	//	MedicineCategory  medicineCategory =categoryService.getCategoryById(categoryid);
		medicineCategory.setCategoryDesc(categoryDesc);
		medicineCategory.setCategoryName(categoryName);
	//	categoryService.editCategory(medicineCategory);
		return SUCCESS;
	}
	/**
	 * 删除药品类别信息
	 * @return
	 * @throws Exception
	 */
	public String deleteCategory() throws Exception{
		HttpServletRequest request =ServletActionContext.getRequest();
		String categoryid =request.getParameter("categoryid");
	//	MedicineCategory medicineCategory =categoryService.getCategoryById(categoryid);
	//	categoryService.deleteCategory(medicineCategory);
		return SUCCESS;
	}

	
	public List<MedicineCategory> ToList(CategoryResponse response) throws ParseException{
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		System.out.println("list:"+list);
		com.mysoap.medicine.service.CategorySoapStub.MedicineCategory[] mcs = response.getMedicineCategorys();
		String[] str = response.getStr1().split(",");
		Calendar cal = Calendar.getInstance();
		for(int i = 0; i<mcs.length;i++){
			MedicineCategory mc = new MedicineCategory();
			mc.setCategoryCreateTime(df.parse(str[i]));
			mc.setCategoryDesc(mcs[i].getCategoryDesc());
			mc.setCategoryId(mcs[i].getCategoryId());
			mc.setCategoryName(mcs[i].getCategoryName());
			if(list==null){
				list = new ArrayList<MedicineCategory>();
				
			}
			list.add(mc);
		}
		return list;
		
	}
}
